/*******************************************************************************
Replication Materials for Blau, Kahn, Brummund, Cook, and Larson-Koester "Is 
There Still Son Preference in the United States?"

Figure 2

Date Modified: 10/19/2019

*******************************************************************************/




*-------------------------------------------------------------------------------
                                   *LOAD DATA
*-------------------------------------------------------------------------------
use "$acs\acs_data", clear  



*-------------------------------------------------------------------------------
                                 *RESTRICT SAMPLE
*-------------------------------------------------------------------------------
keep if nonusbirth == 0 & multi_sample != 1 & mult == 0 & oldc <=12 ///
	& inrange(age,18,40)==1 & nchild18>=1
drop if citizen==1 | marst == 5



*-------------------------------------------------------------------------------
                          *GENERATE DM (2008) VARIABLES
*-------------------------------------------------------------------------------

// RACE:
gen dmrace=0 if inlist(raced,100,120)==1 //White
replace dmrace = 1 if inlist(raced,200,210,801,917,985,986,990)==1 ///
					| inrange(raced,830,845)==1 ///
					| inrange(raced,901,904)==1 ///
					| inrange(raced,930,935)==1 ///
					| inrange(raced,950,955)==1 ///
					| inrange(raced,970,973)==1 ///
					| inrange(raced,980,983)==1
replace	dmrace = 2 if (inlist(raced,902,905,910,933,934,940,941,943,944,950,953,954,960,961)==1 ///
					| inrange(raced,400,679)==1 ///
					| inrange(raced,810,819)==1 ///
					| inrange(raced,831,838)==1 ///
					| inrange(raced,850,854)==1 ///
					| inrange(raced,860,887)==1 ///
					| inrange(raced,910,923)==1 ///
					| inrange(raced,926,930)==1 ///
					| inrange(raced,963,971)==1 ///
					| inrange(raced,973,981)==1 ///
					| inrange(raced,983,986)==1) & dmrace==.
replace dmrace = 3 if dmrace==.
label define dmrace_lbl 0 "White" 1"Black" 2"Asian" 3"Other"
label val dmrace dmrace_lbl		

// EDUCATION:
gen college = scol + cold // combined college variable

// DM DEPENDENT VARS (run on full sample):
sum femhdalt // first dependent variables
gen momnvrmar = marst == 6 & female == 1 // mother never marries
gen divorce = .
replace divorce = 0 if marst>=1 & marst<=4
replace divorce = 1 if (marst == 3 | marst == 4) // divorce or seperation
gen firstdivorce = .
replace firstdivorce = 0 if (female == 1 & marst != 6) // mothers, not never married, widows exluded in sample?
replace firstdivorce = 1 if female == 1 & (marst == 3 | marst == 4 | marrno == 2 | marrno == 3)
gen femhdalt2 = .
replace femhdalt2 = 0 if divorce == 1
replace femhdalt2 = 1 if divorce == 1 & femhdalt == 1
gen evermarried = marst != 6 
gen divsep = (marst == 3 | marst == 4)
gen ksex1 = chld1 == 1 // girl
gen ksex0 = chld1 == 0 // boy
gen custody = 0
replace custody = 1 if femhdalt == 1 // in divroced/seperated sample, they are equal



*-------------------------------------------------------------------------------
               *SET UP EXCEL FILE THAT WILL STORE PERCENT DECOMPOSITION
*-------------------------------------------------------------------------------
putexcel set "$foutput\figure2_data", replace
putexcel A1 = "year" 
putexcel B1 = "never_married" 
putexcel C1 = "custody" 
putexcel D1 = "divorce" 
putexcel A2 = "1960"
putexcel A3 = "1970"
putexcel A4 = "1980"
putexcel A5 = "1990"
putexcel A6 = "2000"
putexcel A7 = "2008-2013"



*-------------------------------------------------------------------------------
                     *PUT DM ESTIMATES INTO THE EXCEL FILE
*-------------------------------------------------------------------------------
*note: Copied from DM (2008) Figure 1

// NEVER MARRIED:
putexcel B2 = 20
putexcel B3 = 14
putexcel B4 = 9
putexcel B5 = 21
putexcel B6 = 22

// CUSTODY:
putexcel C2 = 37
putexcel C3 = 35
putexcel C4 = 68
putexcel C5 = 60
putexcel C6 = 59

// DIVORCE
putexcel D2 = 43
putexcel D3 = 51
putexcel D4 = 23
putexcel D5 = 19
putexcel D6 = 20





*-------------------------------------------------------------------------------
                                 *REGRESSIONS
*-------------------------------------------------------------------------------
local controls "lths college i.cohort" //leave out: hsdi
local controls "`controls' i.dmrace i.region age age2 age3"
local wgt "perwtnorm"
matrix rough = (., ., ., . \ ., ., ., .)
local i = 2
foreach y in momnvrmar divorce femhdalt2 {
		reg `y' chld1 `controls' [aw=`wgt'],  robust
		return list
		matrix rough[1,`i'] = r(table)[1, 1]
		local i = `i' + 1
}


*-------------------------------------------------------------------------------
                             *CALC PROBABILITIES
*-------------------------------------------------------------------------------


// Pr(D|EM,G):
sum divsep if ksex1 == 1 & evermarried == 1 [aw=perwtnorm]
return list
matrix rough[2, 1] = r(mean)

// Pr(MC|D,EM,B):
sum custody if divsep == 1  & evermarried == 1 & ksex0 == 1 [aw=perwtnorm]
return list
matrix rough[2, 2] = r(mean)

// Pr(EM|G):
sum evermarried if ksex1 == 1 [aw=perwtnorm]
return list
matrix rough[2, 3] = r(mean)

// Pr(EM|B):
sum evermarried if ksex0 == 1 [aw=perwtnorm]
return list
matrix rough[2, 4] = r(mean)


*-------------------------------------------------------------------------------
                          *CALC PERCENT DECOMPOSITION
*-------------------------------------------------------------------------------

// NEVER MARRIED:
local nm_share = rough[1,2]*(1- rough[2,1]*rough[2,2])

// CUSTODY:
local cus_share = rough[2,3]*rough[2,1]*rough[1,4]

// DIVORCE:
local div_share = rough[2,2]*rough[2,4]*rough[1,3]

// COMBINED:
local combined = `nm_share' + `cus_share' + `div_share'

// PERCENT EFFECTS STORED
putexcel B7 = 100*`nm_share'/`combined' // NEVER MARRIED
putexcel C7 = 100*`cus_share'/`combined' // CUSTODY
putexcel D7 = 100*`div_share'/`combined' // DIVORCE





*-------------------------------------------------------------------------------
					             *FIGURE CODE
*-------------------------------------------------------------------------------
import excel "$foutput\figure2_data", clear firstrow 

// create the numeric year variable:
egen group = group(year)
tsset group

// two way line (can replace two way connected with line)
twoway connected divorce custody never_married group, ///
xtitle("") ///
xlabel( 1 "1960" 2 "1970" 3 "1980" 4 "1990" 5 "2000" 6 "2008-2013", noticks) ///
ytitle("Share of Component Channels (%)") ///
ysc(r(0 100))  ylabel(0(20)100) ///
graphregion(color(white)) bgcolor(white) ///
legend(order(3 "Never married (%)" 2 "Custody (%)" 1 "Divorce(%)") rows(3)) ///
lwidth(medthick medthick medthick) lpattern(solid dash shortdash) lcolor(black black black) ///
xsc(r(.8 6.3)) ///
mcolor(black black blakc)
graph export "$foutput\figure2.png", replace		
	

